package br.ufpe.cin.msc.jrsj2.recommender.metadata;

import bbc.rd.tvanytime.BasicDescription;
import bbc.rd.tvanytime.ContentReference;
import bbc.rd.tvanytime.Synopsis;
import bbc.rd.tvanytime.TVAnytimeException;
import bbc.rd.tvanytime.Title;
import bbc.rd.tvanytime.programInformation.ProgramInformation;
import bbc.rd.tvanytime.programInformation.ProgramInformationTable;
import br.ufpe.cin.msc.jrsj2.recommender.persistence.dao.DAOFactory;
import br.ufpe.cin.msc.jrsj2.recommender.persistence.entity.Program;

public class TVAnytimeMetadata implements MetadataFormat {

	@Override
	public String buildMetadata(String programID) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory();
		Program program = daoFactory.getProgramDAO().loadByCRID(programID);

		if (program == null)
			return null;

		ContentReference crid = null;
		try {
			crid = new ContentReference(program.getId());
		} catch (TVAnytimeException ex) {
			ex.printStackTrace();
		}

		Title title = new Title(program.getTitle());
		Synopsis synopsis = new Synopsis(program.getSynopsis());
		BasicDescription basicDesc = new BasicDescription();
		basicDesc.addTitle(title);
		basicDesc.addSynopsis(synopsis);

		ProgramInformation progInfo = new ProgramInformation();
		progInfo.setProgramID(crid);
		progInfo.setBasicDescription(basicDesc);

		ProgramInformationTable progInfoTable = new ProgramInformationTable();
		progInfoTable.addProgramInformation(progInfo);

		return progInfoTable.toXML();
	}

}
